package com.study.springboot.jpa.service.impl;

import com.study.springboot.jpa.entity.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.StoredProcedureQuery;
import java.util.List;
@Component
public class ContactsService {
    @Autowired
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public List<Contact> findAllViaProc(String name) {
        StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("getContactsLikeName");
        storedProcedureQuery.setParameter("name", name);
        storedProcedureQuery.execute();
        return storedProcedureQuery.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<Contact> findAllByViaQuery(String name) {
        List<Contact> contacts = this.entityManager
                .createNativeQuery("select id, name, phone, mail from contact where name like :name", "conatctMapping")
                .setParameter("name", name)
                .setMaxResults(5)
                .getResultList();

        return contacts;
    }
}
